package com.hzvdvmc.autosale.setting.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.Toolbar;
import android.widget.ScrollView;
import android.widget.TextView;

import com.hzvdvmc.autosale.MyApp;
import com.hzvdvmc.autosale.R;
import com.hzvdvmc.autosale.base.ActionbarActivity;
import com.hzvdvmc.autosale.util.LogToFileHelper;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.ButterKnife;

public class LogContentActivity extends ActionbarActivity {

    @BindView(R.id.tv_content)
    TextView tvContent;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_func)
    TextView tvFunc;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.scrollView)
    ScrollView scrollView;

    private String fileName;
    private String filePath;
    private String text;
    private Timer timer = new Timer();

    public static void startMe(Context context, String fileName, String filePath) {
        Intent intent = new Intent(context, LogContentActivity.class);
        intent.putExtra("fileName", fileName);
        intent.putExtra("filePath", filePath);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_log_content);
        ButterKnife.bind(this);
        parseIntent();
        setCustomeActiorBar(fileName);

        tvContent.setText(text);

        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                text = LogToFileHelper.ReadFile(filePath);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        tvContent.setText(text);
                    }
                });
            }
        }, 0);

        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        scrollView.fullScroll(ScrollView.FOCUS_DOWN);
                    }
                });
            }
        }, 500);
    }

    private void parseIntent() {
        fileName = getIntent().getStringExtra("fileName");
        filePath = getIntent().getStringExtra("filePath");
    }
}
